package com.bigdata.mapreduce.avg;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/*
AvgCombiner 输出的value，需要有sum，count 2个属性值
 */
public class AvgCombiner extends Reducer<Text, Text, Text, Text> {

    private Text result = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        int sum = 0;
        int count = 0;
        Iterator<Text> iter = values.iterator();
        while (iter.hasNext()) {
            sum += Integer.valueOf(iter.next().toString());
            count++;
        }
        // Combiner 输出的value
        result.set(sum + "_" + count);
        context.write(key, result);
    }
}
